package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commen.Result;
import com.itheima.entiry.*;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries=true)
    public Result add(@RequestBody SetmealDto setmealDto){
        setmealService.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishService.save(setmealDish);
        }
        return new Result(1);
    }

    @GetMapping("/page")
    public Result getPages(Integer page,Integer pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(name!=null,Setmeal::getName,name);
        Page<Setmeal> setmealpages = setmealService.page(setmealPage, qw);
        List<Setmeal> records = setmealpages.getRecords();
        int count = setmealService.count(qw);
        List<SetmealDto> setmealDtos=new ArrayList<>();
        for (Setmeal setmeal : records) {
            //把setmeal复制给setmealDto
            //把CategoryName赋值给setmealDto
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Long categoryId = setmeal.getCategoryId();
            Category categoryServiceById = categoryService.getById(categoryId);
            String categoryServiceByIdName = categoryServiceById.getName();
            setmealDto.setCategoryName(categoryServiceByIdName);
            setmealDtos.add(setmealDto);
        }
        Page<SetmealDto> returnPage = new Page<>();
        returnPage.setRecords(setmealDtos);
        returnPage.setTotal(count);

        return new Result(1,returnPage,null);
    }
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries=true)
    public Result delete(@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        setmealService.remove(qw);
        Setmeal setmealServiceById = setmealService.getById(qw);
        Long setmealServiceByIdId = setmealServiceById.getId();
        LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(SetmealDish::getSetmealId,setmealServiceByIdId);
        setmealDishService.remove(qw2);
        return new Result(1);
    }

    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealService.update(setmeal,qw);
        return new Result(1);
    }

    @GetMapping("/{id}")
        public Result getById(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
         SetmealDto setmealDto = new SetmealDto();
        //把setmealGetById中的数据复制到setmealDto中
        Setmeal setmealGetById = setmealService.getById(id);
        BeanUtils.copyProperties(setmealGetById,setmealDto);
        //获取categoryName赋值给setmealDto
//        Long categoryId = setmealGetById.getCategoryId();
//        String categoryName = categoryService.getById(categoryId).getName();
//        setmealDto.setCategoryName(categoryName);
        //获取setmealDish中SetmealId为指定id的集合
        List<SetmealDish> setmealDishLists = setmealDishService.list(setmealDishLambdaQueryWrapper);
        //获取SetmealDish集合赋值给setmealDto
        setmealDto.setSetmealDishes(setmealDishLists);
        return new Result(1,setmealDto,null);
    }

    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries=true)
    public Result update(@RequestBody SetmealDto setmealDto){
//        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        setmealLambdaQueryWrapper.eq(Setmeal::getId,setmealDto.getId());
        setmealService.updateById(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishService.updateBatchById(setmealDishes);
        return new Result(1);
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache" , key = "'setmeal_'+#p0")
    public Result getById(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        List<Setmeal> setmeallists = setmealService.list(qw);
        return new Result(1,setmeallists);
    }
}
